/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.gui.security;

import it.polimi.registration.business.security.boundary.EventManager;
import it.polimi.registration.business.security.boundary.UsersEventManager;
import it.polimi.registration.business.security.boundary.UsersManager;
import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Users;
import it.polimi.registration.business.security.entity.UsersEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.glassfish.tyrus.core.RequestContext;
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author Luca
 */
@Named
@SessionScoped
public class EventBean implements Serializable {

    private UsersEvent usersEvent;
    private List<Long> ids;
    FacesContext c;
    @Inject
    private ForecastBean fb ;
    
    
    RequestContext r;

    /**
     * @param usersEvent the usersEvent to set
     */
    public void setUsersEvent(UsersEvent usersEvent) {
        this.usersEvent = usersEvent;
    }

    public void addUsersEvent() {
        getUem().save(getUsersEvent());
    }

    private Event event;
    private UsersEvent ue = new UsersEvent();
    private boolean canModify = false;

    private List<Users> SelectedUsers;

    public List<Users> getSelectedUsers() {
        return SelectedUsers;
    }

    public void setSelectedUsers(List<Users> SelectedUsers) {
        this.SelectedUsers = SelectedUsers;
    }

    public List<Users> getListUsers() {
        return um.getListUsers();
    }

    public boolean isCanModify() {
        return canModify;
    }

    public void setCanModify(boolean canModify) {
        this.canModify = canModify;
    }

    @EJB
    private EventManager evm;
    @EJB
    private UsersEventManager uem;
    @EJB
    private UsersManager um;

    
    public EventBean() {

    }

    public Event getEvent() {
        if (event == null) {
            event = new Event();
        }

        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public void sendInvitation(Event event, Users users) {
        setUsersEvent(new UsersEvent());
        getUsersEvent().setUsers(users);
        getUsersEvent().setEvent(event);
        getUsersEvent().setCreator(false);
        getUsersEvent().setAccepted(Boolean.FALSE);
        getUsersEvent().setSeen(Boolean.FALSE);
        addUsersEvent();
    }

    public void addEvent() {

        ids = getEvm().loadAllEventById();
        if (ids.contains(event.getId())) {
            getEvm().updateEvent(event);
        } else {
            event.setStartDate(event.mergedata(event.getStartDate(), event.getStartH()));
            event.setEndDate(event.mergedata(event.getEndDate(), event.getEndH()));
            getEvm().save(event);
            setUsersEventCreator();
            setSelectedUsers(um.getListUsers());
            for (Users SelectedUsers1 : this.SelectedUsers) {
                this.sendInvitation(event, SelectedUsers1);
            }
        }
           
            fb.checkOneEventWeather(event);
            event = new Event();
        
    }
    
    public void addEvent(Event e) {
        getEvm().save(e);
    }

    public void onEventSelect(SelectEvent selectEvent) {
        DefaultScheduleEvent eventTemp = (DefaultScheduleEvent) selectEvent.getObject();
        event = getEvm().getEventByID(eventTemp.getDescription());
        canModify = getUem().isCreator(event, um.getLoggedUsers());
    }

    public void onDateSelect(SelectEvent selectEvent) {
        event = new Event();
        canModify = true;
        event = new Event();
        DefaultScheduleEvent selectedEvent = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
        event.setStartDate(selectedEvent.getStartDate());
        event.setEndDate(selectedEvent.getStartDate());
    }

    public void setUsersEventCreator() {
        ue.setUsers(getUm().getLoggedUsers());
        ue.setEvent(event);
        ue.setCreator(true);
        ue.setAccepted(false);
        ue.setSeen(false);
            getUem().save(ue);
        ue = new UsersEvent();
    }

    /**
     * @return the um
     */
    public UsersManager getUm() {
        return um;
    }

    /**
     * @param um the um to set
     */
    public void setUm(UsersManager um) {
        this.um = um;
    }

    public void deleteEvent() {
        getUem().removeAllInvitationToAnEvent(event);
        getEvm().deleteEvent(event);
    }

    /**
     * @return the evm
     */
    public EventManager getEvm() {
        return evm;
    }

    /**
     * @param evm the evm to set
     */
    public void setEvm(EventManager evm) {
        this.evm = evm;
    }

    /**
     * @return the uem
     */
    public UsersEventManager getUem() {
        return uem;
    }

    /**
     * @param uem the uem to set
     */
    public void setUem(UsersEventManager uem) {
        this.uem = uem;
    }

    /**
     * @return the usersEvent
     */
    public UsersEvent getUsersEvent() {
        return usersEvent;
    }

}
